package com.itheima.controller;

import com.itheima.flag.Flag;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController

@RequestMapping
public class EmpController {
    @Autowired
    private EmpService empService;

//    @GetMapping("/emps")
//    public Result getEmp(
//            @RequestParam(defaultValue = "1") Integer page ,
//            @RequestParam(defaultValue = "10") Integer pageSize
//    ){
//        PageResult<Emp> pageResult = empService.getEmp(page, pageSize);
//        return Result.success(pageResult);
//    }

//    @GetMapping
//    public Result getEmp(
//            String name ;
//            Integer gender;
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin ,
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end ,
//            @RequestParam(defaultValue = "1") Integer page ,
//            @RequestParam(defaultValue = "10") Integer pageSize){
//        PageResult<Emp> pageResult = empService.getEmp(page, pageSize);
//        return Result.success(pageResult);
//    }


    @GetMapping("/emps")
    public Result getEmp(
            String name,
            Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end,
            @RequestParam(defaultValue = "1")
            Integer page,
            @RequestParam(defaultValue = "10")
            Integer pageSize) {
        PageResult<Emp> pageResult = empService.getEmp(name, gender, begin, end, page, pageSize);
        return Result.success(pageResult);
    }

    @Flag
    //@RequestBody 是将前端传输过来的json对象封装成实体类对象
    @PostMapping("/emps")
    public Result insertEmp(@RequestBody Emp emp) throws Exception {
        empService.insertEmp(emp);
        return Result.success();
    }
    @Flag
    @DeleteMapping("/emps")
    public Result deleteEmp(@RequestParam List<Integer> ids) {
        empService.deleteEmp(ids);
        return Result.success();
    }

    //修改前的数据回显
    @GetMapping("/emps/{id}")
    public Result queryById(@PathVariable Integer id) {
        Emp emp = empService.queryById(id);
        return Result.success(emp);
    }
    @Flag
    //开始修改的数据
    @PutMapping("/emps")
    public Result updateEmp(@RequestBody Emp emp) {
        empService.updateEmp(emp);
        return Result.success();
    }

    //查询所有数据
    @GetMapping("/emps/list")
    public Result queryAllEmp() {
        List<Emp> list = empService.queryAllEmp();
        return Result.success(list);
    }


}
